package com.atguigu.gmall.user.rest;

import com.atguigu.gmall.common.constant.RedisConstant;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.IpUtil;
import com.atguigu.gmall.model.user.vo.LoginParamsVO;
import com.atguigu.gmall.model.user.vo.LoginResultVO;
import com.atguigu.gmall.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/user")
public class LoginController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 登录
     * @param loginParamsVO
     * @return
     */
    @PostMapping("/passport/login")
    public Result doLogin(@RequestBody LoginParamsVO loginParamsVO, HttpServletRequest request) {
        String ipAddress = IpUtil.getIpAddress(request);
        LoginResultVO res = userInfoService.doLogin(loginParamsVO,ipAddress);
        return Result.ok(res);
    }
    @GetMapping("/passport/logout")
    public Result doLogout(@RequestHeader(value = "Token",required = false) String token) {
        stringRedisTemplate.delete(RedisConstant.REDIS_USER_INFO_PREFIX + token);
        return Result.ok();
    }
}
